package com.manage.server.controller;


import com.manage.server.pojo.Income;
import com.manage.server.pojo.QueryIncome;
import com.manage.server.pojo.RespBean;
import com.manage.server.service.IncomeService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * <p>
 * 前端控制器
 * </p>
 *
 * @author zl
 * @since 2021-05-26
 */
@RestController
@RequestMapping("income")
public class IncomeController {
    @Autowired
    private IncomeService incomeService;
    /**
     * <p>
     *  获得所有的收支情况
     * </p>
     *
     * @author zl
     * @since 2021-05-26
     */
    @GetMapping("getAllIncome")
    public List<Income> getAllIncome() {
        return incomeService.getAllIncome();
    }
    /**
     * <p>
     *  更新收支
     * </p>
     *
     * @author zl
     * @since 2021-05-26
     */
    @PutMapping("updateIncome")
    public RespBean updateIncome(@RequestBody Income income) {
        if (incomeService.updateIncome(income)){
            return RespBean.success("更新成功!");
        }else {
            return RespBean.error("更新失败!");
        }

    }
    /**
     * <p>
     *  根据id删除收支
     * </p>
     *
     * @author zl
     * @since 2021-05-26
     */
    @DeleteMapping("deleteIncomeById/{id}")
    public RespBean deleteIncomeById(@PathVariable Integer id) {
        if (incomeService.deleteIncomeById(id)){
            return RespBean.success("删除成功!");
        }else {
            return RespBean.error("删除失败!");
        }

    }
    /**
     * <p>
     *  添加收支
     * </p>
     *
     * @author zl
     * @since 2021-05-26
     */
    @PostMapping("addIncome")
    public RespBean addIncome(@RequestBody Income income) {
        if (incomeService.addIncome(income)){
            return RespBean.success("添加成功!");
        }else {
            return RespBean.error("添加失败!");
        }
    }
    /**
     * <p>
     *  根据条件查询收支
     * </p>
     *
     * @author zl
     * @since 2021-05-26
     */
    @PostMapping("queryIncome")
    public List<Income> queryIncome(@RequestBody QueryIncome queryIncome){
        return incomeService.getIncomes(queryIncome);
    }
}
